package com.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.Map;

/**
 * 测试发布时间
 */

/**
 * 使用Jsoup.connect()访问页面获取org.jsoup.Connection，再设置请求头，再用Jsoup.parse()进行解析
 */
public class Demo5 {

    public static void main(String[] args) {
        try {
            // 获取连接
//            Connection connect = Jsoup.connect("https://sapi.sina.cn/search/list?newsId=HB-1-snhs%2Findex_v2-search&page=1&newspage=0&keyword=%25E7%25A2%25B3&tab=news&sort=0&is_ec=0&_=1625742980643&callback=Zepto1625742681918");
            Connection connect = Jsoup.connect("https://sapi.sina.cn/search/list?newsId=HB-1-snhs%2Findex_v2-search&page=1&newspage=0&keyword=%25E7%25A2%25B3&tab=news&sort=0&is_ec=0&_=1625821851186&callback=Zepto1625821848976");
            // 解析页面
            Document document = connect.get();
            Element body = document.getElementsByTag("body").first();
            String text = body.text();
//            System.out.println(text);

            String json = text.substring(text.indexOf("{"), text.lastIndexOf(")"));
//            System.out.println(json);

            ObjectMapper mapper = new ObjectMapper();
            Map map = mapper.readValue(json, Map.class);
            Map data = (Map) map.get("data");
            List<Map<String, String>> feed = (List) data.get("feed");
            int index = 0;
            for (Map<String, String> item : feed) {
                System.out.println("=======================================================");
                // 标题
                String title = item.get("title");
//                System.out.println(title);
                // 来源
                String source = item.get("source");
//                System.out.println(source);
                // 封面图片
                String kpic = item.get("kpic");
                if (kpic != null && kpic.length() > 0) {
//                    System.out.println(kpic);
                }
                // 资讯链接
                String link = item.get("link");
                if (link != null && link.length() > 0) {
//                    System.out.println(link);
                    // 爬取link
                    fetchPage(link);
                }
//                System.out.println(link);
                System.out.println(index++ + "=======================================================");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fetchPage(String link) {
        try {
            // 获取连接
            Connection connect = Jsoup.connect(link);
            // 解析页面
            Document document = connect.get();
//            Element root = document.getElementsByClass("j_main_art").first();
            Element root = document.getElementsByTag("article").first();

            // 标题
            Element titleElement = root.getElementsByClass("art_tit_h1").first();
//            System.out.println(titleElement.text());

            // 作者
            Element authorElement = root.getElementsByClass("weibo_user").first();
//            System.out.println(authorElement.text());

            // 发布时间
            Element dayElement = root.getElementsByClass("weibo_time_day").first();
            String day = dayElement.text();
//            System.out.println(day);
            Element timeElement = root.getElementsByClass("weibo_time").first();
            String time = timeElement.text();
//            System.out.println(time);


            // 段落
//            Elements paragraphs = root.getElementsByClass("art_pic_card art_content");
//            for (Element paragraph : paragraphs) {
//                System.out.println(paragraph);
//                Element img = paragraph.getElementsByTag("img").first();
//                System.out.println("【图片】" + img);
//            }

            Elements paragraphs = root.getElementsByClass("art_pic_card art_content").first().children();
            StringBuilder stringBuilder = new StringBuilder();
            for (Element paragraph : paragraphs) {
                System.out.println(paragraph);
//                Element img = paragraph.getElementsByTag("img").first();
//                if (img != null) {
//                    System.out.println("有图");
//                    img.attr("src", "www.baidu.com");
//                    System.out.println("【修改后】" + paragraph);
//                }
                Elements imgs = paragraph.getElementsByTag("img");
                if (imgs.size() > 0) {
                    for (Element img : imgs) {
                        System.out.println("【src】" + imgs.attr("src"));
                        System.out.println("【data-src】" + imgs.attr("data-src"));
                    }
                }
                System.out.println("【imgs】" + imgs.size());
            }
//            System.out.println(stringBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("【完成】");
    }
}
